package com.example.uniapp.dao;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.uniapp.bean.CookbookBean;
import com.example.uniapp.bean.CookbookFoodBean;
import com.example.uniapp.db.DBUtils;

import java.util.ArrayList;
import java.util.List;

public class CookbookFoodDao {

    public static SQLiteDatabase db = DBUtils.con;

    /**
     * 根据食谱id获取该食谱用到的食材信息
     * @param cookbookId
     * @return
     */
    public static List<CookbookFoodBean> getCookbookFoodByCookbookId(String cookbookId) {
        List<CookbookFoodBean> cookbookFoodBeanList = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from cookbookfood where cookbook_id=?", new String[]{cookbookId});
        while (cursor.moveToNext()) {
            @SuppressLint("Range") String cookbookFood = cursor.getString(cursor.getColumnIndex("cookbook_food"));
            @SuppressLint("Range") String cookbookFoodCount = cursor.getString(cursor.getColumnIndex("cookbook_food_count"));
            CookbookFoodBean cookbookFoodBean = new CookbookFoodBean(cookbookId, cookbookFood, cookbookFoodCount);
            cookbookFoodBeanList.add(cookbookFoodBean);
        }
        return cookbookFoodBeanList;
    }

    /**
     * 插入食谱用到的食材信息
     * @param cookbookId
     * @param cookbookFood
     * @param cookbookFoodCount
     * @return
     */
    public static int addCookbookFood(String cookbookId, String cookbookFood, String cookbookFoodCount) {
        String[] data = {cookbookId, cookbookFood, cookbookFoodCount};
        try {
            db.execSQL("INSERT INTO cookbookfood (cookbook_id, cookbook_food, cookbook_food_count)" +
                    "VALUES(?, ?, ?)",
                    data);
            return 1;
        }catch (Exception e) {
            return 0;
        }
    }

    /**
     * 根据食谱id删除它的食材信息
     * @param cookbookId
     * @return
     */
    public static int deleteCookbookFood(String cookbookId) {
        try {
            db.execSQL("delete from cookbookfood where cookbook_id=?", new String[] {cookbookId});
            return 1;
        }catch (Exception e) {
            return 0;
        }
    }
}
